/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResourcesView.java
 *
 * Created on 27.05.2013, 17:52:03
 */
package db21;

import db21.entities.CommonEducationalResourceTypes;
import db21.entities.Competences;
import db21.entities.DigitalEducationalResourceTypes;
import db21.entities.EducationalResource;
import db21.entities.EducationalResources;
import db21.entities.Entities;
import db21.entities.Subjects;
import db21.entities.Themes;
import db21.listeners.ClickOnlyListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;

/**
 *
 * @author Mamontov
 */
public class ResourcesView extends ApplicationPanel {
    /**
     * A resource, toggled by ID
     */
    EducationalResource m_resource;
    
    
    
    
    /** Creates new form ResourcesView */
    public ResourcesView() {
        initComponents();
        m_resource = null;
        JButton btns[] = {btnToMain, btnToThemes, btnToCollections, 
                        btnToCompetences};
        ApplicationMode modes[] = { 
            ApplicationMode.MainForm,
            ApplicationMode.Themes,
            ApplicationMode.Collections,
            ApplicationMode.Competences
        };
        for(int i=0; i < btns.length; i++) {
            this.bindOnClickListenerTogglingToMode(btns[i], modes[i]);
        }
        bindSave(btnSave);
        JList lists[] = { 
            lstAllResources, 
            lstAllThemes, lstSubjectThemes,  lstSubthemesOfTheme, 
            lstAllCompetences, lstCompetencesBySubject, 
        };
        for(int i = 0 ; i < lists.length; i++) {
            DefaultListModel m = new DefaultListModel();
            lists[i].setModel(m);
        }
        
        btnDelete.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                JList c = ResourcesView.this.lstAllResources;
                DefaultListModel k = (DefaultListModel)(ResourcesView.this.lstAllResources.getModel());
                int si = c.getSelectedIndex();
                if (si != -1) {
                    String o = c.getSelectedValue().toString();
                    ResourcesView.this.m_model.resource(EducationalResources.class).removeIndividual(o);
                    k.remove(si);
                    if (si < k.size()) {
                        c.setSelectedIndex(si);
                    }
                }
            }
        });
        
        btnAdd.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                EducationalResource t = ResourcesView.this.newResource();
                boolean ok = t!=null;
                if (ok) {
                    ok = t.name.length() != 0;
                }
                if (ok) {
                    DefaultListModel k = (DefaultListModel)(ResourcesView.this.lstAllResources.getModel());
                    boolean result = m_model.resource(EducationalResources.class).add(t);
                    if (result) {
                        k.addElement(t.name);
                    } else {
                         JOptionPane.showMessageDialog(ResourcesView.this, "Individual already exists");
                    }
                }
                else {
                    JOptionPane.showMessageDialog(ResourcesView.this, "Please fill type and name");
                }
                        
            }
        });
    }
    
    EducationalResource newResource()  {
        EducationalResource result = new EducationalResource();
        result.name = txtName.getText();
        JComboBox typeBox;
        if (panAllTypes.getSelectedIndex() == 0) {
            result.typeName = "CommonEducationalResource";
            typeBox = cmbCommonResourceType;
        } else {
            result.typeName = "DigitalEducationalResource";
            typeBox = cmbDigitalResourceType;
        }
        // Type must always be filled
        if (typeBox.getSelectedIndex() < 0) return null;
        result.typeValue = typeBox.getSelectedItem().toString();
        
        // Fill themes
        JList themelist = null;
        if (panThemes.getSelectedIndex() == 0) {
           themelist = lstAllThemes;
        } else {
            themelist = (panThemes.getSelectedIndex() == 1)? lstSubthemesOfTheme : lstSubjectThemes;
        }
        result.themes = Entities.selectedItemsToEntities(themelist);
        
        // Fill competences
        boolean cftab = panCompetences.getSelectedIndex() == 0;
        JList competencelist = (cftab)? lstAllCompetences: lstCompetencesBySubject;
        result.competences = Entities.selectedItemsToEntities(competencelist);
        
        return result;
    }
    
    public void updateWidgetsDueAChangedSelection() {
        if (m_resource != null) {
            txtName.setText(m_resource.name);
            JComboBox  c =  null;
            if (m_resource.typeName.equals("CommonEducationalResource")) {
                c = cmbCommonResourceType;
                panAllTypes.setSelectedIndex(0);
            } else {
                c = cmbDigitalResourceType;
                panAllTypes.setSelectedIndex(1);
            }
            c.setSelectedIndex(Entities.findComboBoxItem(c, m_resource.typeValue));
            
            Entities.setSelectedValues(lstAllThemes, m_resource.themes);
            Entities.setSelectedValues(lstSubjectThemes, m_resource.themes);
            Entities.setSelectedValues(lstSubthemesOfTheme, m_resource.themes);
            
            
            Entities.setSelectedValues(lstAllCompetences, m_resource.competences);
            Entities.setSelectedValues(lstCompetencesBySubject, m_resource.competences);
            
        }
    }
    
    @Override
    public void fireModelUpdate()  {
        
        this.m_model
            .resource(CommonEducationalResourceTypes.class)
            .populateComboBoxWithEntities(cmbCommonResourceType);
        this.m_model
            .resource(DigitalEducationalResourceTypes.class)
            .populateComboBoxWithEntities(cmbDigitalResourceType);
        this.m_model
            .resource(EducationalResources.class)
            .populateListWithEntities(lstAllResources);
        this.m_model
            .resource(Themes.class)
            .populateListWithEntities(lstAllThemes);
        this.m_model
            .resource(Themes.class)
            .populateComboBoxWithEntities(cmbParentTheme);
        this.m_model
            .resource(Subjects.class)
            .populateComboBoxWithEntities(cmbSubjects);
        this.m_model
            .resource(Competences.class)
            .populateListWithEntities(lstAllCompetences);  
        this.m_model
            .resource(Subjects.class)
            .populateComboBoxWithEntities(cmbSubjectCompetences);
        
        this.cmbParentThemeActionPerformed(null);
        this.cmbSubjectsActionPerformed(null);
        this.cmbSubjectCompetencesActionPerformed(null);
        updateWidgetsDueAChangedSelection();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnToMain = new javax.swing.JButton();
        btnToCompetences = new javax.swing.JButton();
        btnToThemes = new javax.swing.JButton();
        btnToCollections = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstAllResources = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        panThemes = new javax.swing.JTabbedPane();
        tabAllTheme = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        lstAllThemes = new javax.swing.JList();
        panSubThemes = new javax.swing.JPanel();
        cmbParentTheme = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSubthemesOfTheme = new javax.swing.JList();
        panSubjectThemes = new javax.swing.JPanel();
        cmbSubjects = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstSubjectThemes = new javax.swing.JList();
        panAllTypes = new javax.swing.JTabbedPane();
        panCommonResource = new javax.swing.JPanel();
        cmbCommonResourceType = new javax.swing.JComboBox();
        panDigitalResourceType = new javax.swing.JPanel();
        cmbDigitalResourceType = new javax.swing.JComboBox();
        panCompetences = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstAllCompetences = new javax.swing.JList();
        panCompetenceBySubject = new javax.swing.JPanel();
        cmbSubjectCompetences = new javax.swing.JComboBox();
        lstSubjectCompetences = new javax.swing.JScrollPane();
        lstCompetencesBySubject = new javax.swing.JList();
        btnSave = new javax.swing.JButton();

        setToolTipText("name");
        setName("name"); // NOI18N

        btnToMain.setText("К главному окну");
        btnToMain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToMainActionPerformed(evt);
            }
        });

        btnToCompetences.setText("К компетенциям");

        btnToThemes.setText("К темам");

        btnToCollections.setText("К коллекциям");

        lstAllResources.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstAllResources.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstAllResourcesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstAllResources);

        btnAdd.setText("Добавить");

        btnDelete.setText("Удалить");

        lblName.setText("Название");

        jScrollPane5.setViewportView(lstAllThemes);

        javax.swing.GroupLayout tabAllThemeLayout = new javax.swing.GroupLayout(tabAllTheme);
        tabAllTheme.setLayout(tabAllThemeLayout);
        tabAllThemeLayout.setHorizontalGroup(
            tabAllThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabAllThemeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addContainerGap())
        );
        tabAllThemeLayout.setVerticalGroup(
            tabAllThemeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabAllThemeLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panThemes.addTab("Все темы", tabAllTheme);

        cmbParentTheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbParentThemeActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lstSubthemesOfTheme);

        javax.swing.GroupLayout panSubThemesLayout = new javax.swing.GroupLayout(panSubThemes);
        panSubThemes.setLayout(panSubThemesLayout);
        panSubThemesLayout.setHorizontalGroup(
            panSubThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panSubThemesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panSubThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addComponent(cmbParentTheme, javax.swing.GroupLayout.Alignment.LEADING, 0, 289, Short.MAX_VALUE))
                .addContainerGap())
        );
        panSubThemesLayout.setVerticalGroup(
            panSubThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panSubThemesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbParentTheme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addContainerGap())
        );

        panThemes.addTab("Подтемы темы", panSubThemes);

        cmbSubjects.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSubjectsActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(lstSubjectThemes);

        javax.swing.GroupLayout panSubjectThemesLayout = new javax.swing.GroupLayout(panSubjectThemes);
        panSubjectThemes.setLayout(panSubjectThemesLayout);
        panSubjectThemesLayout.setHorizontalGroup(
            panSubjectThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panSubjectThemesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panSubjectThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addComponent(cmbSubjects, javax.swing.GroupLayout.Alignment.LEADING, 0, 289, Short.MAX_VALUE))
                .addContainerGap())
        );
        panSubjectThemesLayout.setVerticalGroup(
            panSubjectThemesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panSubjectThemesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbSubjects, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addContainerGap())
        );

        panThemes.addTab("Темы предмета", panSubjectThemes);

        javax.swing.GroupLayout panCommonResourceLayout = new javax.swing.GroupLayout(panCommonResource);
        panCommonResource.setLayout(panCommonResourceLayout);
        panCommonResourceLayout.setHorizontalGroup(
            panCommonResourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCommonResourceLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbCommonResourceType, 0, 289, Short.MAX_VALUE)
                .addContainerGap())
        );
        panCommonResourceLayout.setVerticalGroup(
            panCommonResourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCommonResourceLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbCommonResourceType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        panAllTypes.addTab("Общий обр. ресурс", panCommonResource);

        javax.swing.GroupLayout panDigitalResourceTypeLayout = new javax.swing.GroupLayout(panDigitalResourceType);
        panDigitalResourceType.setLayout(panDigitalResourceTypeLayout);
        panDigitalResourceTypeLayout.setHorizontalGroup(
            panDigitalResourceTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDigitalResourceTypeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbDigitalResourceType, 0, 289, Short.MAX_VALUE)
                .addContainerGap())
        );
        panDigitalResourceTypeLayout.setVerticalGroup(
            panDigitalResourceTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDigitalResourceTypeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbDigitalResourceType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        panAllTypes.addTab("Цифровой обр. ресурс", panDigitalResourceType);

        jScrollPane4.setViewportView(lstAllCompetences);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                .addContainerGap())
        );

        panCompetences.addTab("Все компетенции", jPanel1);

        cmbSubjectCompetences.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSubjectCompetencesActionPerformed(evt);
            }
        });

        lstSubjectCompetences.setViewportView(lstCompetencesBySubject);

        javax.swing.GroupLayout panCompetenceBySubjectLayout = new javax.swing.GroupLayout(panCompetenceBySubject);
        panCompetenceBySubject.setLayout(panCompetenceBySubjectLayout);
        panCompetenceBySubjectLayout.setHorizontalGroup(
            panCompetenceBySubjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panCompetenceBySubjectLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panCompetenceBySubjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lstSubjectCompetences, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addComponent(cmbSubjectCompetences, javax.swing.GroupLayout.Alignment.LEADING, 0, 284, Short.MAX_VALUE))
                .addContainerGap())
        );
        panCompetenceBySubjectLayout.setVerticalGroup(
            panCompetenceBySubjectLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCompetenceBySubjectLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbSubjectCompetences, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lstSubjectCompetences, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addContainerGap())
        );

        panCompetences.addTab("Компетенции по предмету", panCompetenceBySubject);

        btnSave.setText("Сохранить");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(panThemes, 0, 0, Short.MAX_VALUE)
                                .addComponent(panAllTypes, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(panCompetences, 0, 0, Short.MAX_VALUE)))
                        .addGap(35, 35, 35))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(lblName)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnToCompetences, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnToCollections, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnToMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnToThemes, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))))
                        .addContainerGap(29, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblName)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panAllTypes, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(panThemes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panCompetences, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnToCollections)
                            .addComponent(btnToThemes)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnDelete)
                    .addComponent(btnSave)
                    .addComponent(btnToCompetences)
                    .addComponent(btnToMain))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("sladlkdsdas");
    }// </editor-fold>//GEN-END:initComponents

    private void btnToMainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToMainActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnToMainActionPerformed

    private void cmbSubjectCompetencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSubjectCompetencesActionPerformed
       if (cmbSubjectCompetences.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String subject = cmbSubjectCompetences.getSelectedItem().toString();
            Competences c = m_model.resource(Competences.class);
            ArrayList<String> s = c.listRelatedToSubject(subject);
            Entities.populateListWithStrings(lstCompetencesBySubject, s);
            if (m_resource != null) {
                Entities.setSelectedValues(lstCompetencesBySubject, m_resource.competences);
            }
        }
    }//GEN-LAST:event_cmbSubjectCompetencesActionPerformed

    private void cmbParentThemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbParentThemeActionPerformed
        if (cmbParentTheme.getSelectedIndex() != -1) {
            // lstSubthemesOfTheme
            String parentTheme = cmbParentTheme.getSelectedItem().toString();
            Themes c = m_model.resource(Themes.class);
            ArrayList<String> s = c.listSubthemes(parentTheme);
            Entities.populateListWithStrings(lstSubthemesOfTheme, s);
            if (m_resource != null) {
                Entities.setSelectedValues(lstSubthemesOfTheme, m_resource.themes);
            }
        }
    }//GEN-LAST:event_cmbParentThemeActionPerformed

    private void cmbSubjectsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSubjectsActionPerformed
        if (cmbSubjects.getSelectedIndex() != -1) {
            String subject = cmbSubjects.getSelectedItem().toString();
            Themes c = m_model.resource(Themes.class);
            ArrayList<String> s = c.listRelatedToSubject(subject);
            Entities.populateListWithStrings(lstSubjectThemes, s); 
            if (m_resource != null) {
                Entities.setSelectedValues(lstSubjectThemes, m_resource.themes);
            }
        }
    }//GEN-LAST:event_cmbSubjectsActionPerformed

    private void lstAllResourcesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstAllResourcesValueChanged
        if (lstAllResources.getSelectedIndex() != -1) {
            String data = lstAllResources.getSelectedValue().toString();
            EducationalResource r  = m_model.resource(EducationalResources.class)
                                            .fetch(data);
            m_resource = r;
            updateWidgetsDueAChangedSelection();
        } else {
            m_resource = null;
        }
    }//GEN-LAST:event_lstAllResourcesValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnToCollections;
    private javax.swing.JButton btnToCompetences;
    private javax.swing.JButton btnToMain;
    private javax.swing.JButton btnToThemes;
    private javax.swing.JComboBox cmbCommonResourceType;
    private javax.swing.JComboBox cmbDigitalResourceType;
    private javax.swing.JComboBox cmbParentTheme;
    private javax.swing.JComboBox cmbSubjectCompetences;
    private javax.swing.JComboBox cmbSubjects;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lblName;
    private javax.swing.JList lstAllCompetences;
    private javax.swing.JList lstAllResources;
    private javax.swing.JList lstAllThemes;
    private javax.swing.JList lstCompetencesBySubject;
    private javax.swing.JScrollPane lstSubjectCompetences;
    private javax.swing.JList lstSubjectThemes;
    private javax.swing.JList lstSubthemesOfTheme;
    private javax.swing.JTabbedPane panAllTypes;
    private javax.swing.JPanel panCommonResource;
    private javax.swing.JPanel panCompetenceBySubject;
    private javax.swing.JTabbedPane panCompetences;
    private javax.swing.JPanel panDigitalResourceType;
    private javax.swing.JPanel panSubThemes;
    private javax.swing.JPanel panSubjectThemes;
    private javax.swing.JTabbedPane panThemes;
    private javax.swing.JPanel tabAllTheme;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
